package com.health.adapter;

import java.util.List;

import com.health.R;
import com.health.tablebean.JTYSDoctorContInfoBean;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

/**
 * 签约Adapter
 */
public class QYAdapter3 extends BaseAdapter {

	private Context mContext;
	public static final String QYINFO_JC = "基础服务";
	List<JTYSDoctorContInfoBean> list = null;
	private static SparseBooleanArray isSelected;
	private  int flagSave;
	/** 用SparseBooleanArray来代替map 
	 * @param flagSave **/

	public QYAdapter3(Context mContext, List<JTYSDoctorContInfoBean> jtfwxmbs, int flagSave) {
		this.mContext = mContext;
		this.list = jtfwxmbs;
		isSelected = new SparseBooleanArray();
		initData();
		this.flagSave = flagSave;
	}

	private void initData() {
		int size = list.size();
		if (size > 0) {
			for (int i = 0; i < size; i++) {
				getIsSelected().put(i, false);
			}
		}

	}

	public static SparseBooleanArray getIsSelected() {
		return isSelected;
	}

	public static void setIsSelected(SparseBooleanArray isSelected) {
		QYAdapter3.isSelected = isSelected;
	}

	@Override
	public int getCount() {
		return list.size();

	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder vh = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.qyinfo_list_item, null);
			vh = new ViewHolder();
			findView(vh, convertView);
			convertView.setTag(R.id.TAG_VIEWHOLDER, vh);
		} else {
			vh = (ViewHolder) convertView.getTag(R.id.TAG_VIEWHOLDER);
		}

		if (position % 2 == 0) {
			convertView.setBackgroundColor(mContext.getResources().getColor(R.color.list_bg1));
		} else {
			convertView.setBackgroundColor(mContext.getResources().getColor(R.color.list_bg2));
		}
		final JTYSDoctorContInfoBean fwxmb = list.get(position);
		vh.tongji_list_checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				// holderView.cb_button.toggle();
				if (buttonView.isPressed()) {
					isSelected.put(position, isChecked);
				}
				fwxmb.setSelected(isChecked);
			}
		});

		if (isSelected.get(position)) {
			vh.tongji_list_checkbox.setChecked(true);
		}else{
			vh.tongji_list_checkbox.setChecked(false);
		}
		setData(vh, convertView, fwxmb);
		if (flagSave == 1) {
			vh.tongji_list_checkbox.setEnabled(false);
			vh.tongji_list_checkbox.setClickable(false);
		}
		return convertView;
	}

	/**
	 * 赋值
	 * 
	 * @param h
	 * @param v
	 * @param position
	 * @author 王婷婷
	 * @date 2015年12月3日 上午10:24:28
	 */
	private void setData(ViewHolder h, View v, JTYSDoctorContInfoBean fwxmb) {
		if (fwxmb == null) {
			return;
		}

		h.tongji_list_xmb.setText(fwxmb.getContractServiceItemType());
		h.tongji_list_content.setText(fwxmb.getContractServiceItemContent());
		h.tongji_list_fwks.setText(fwxmb.getContractServiceItemKs());

	}

	private void findView(ViewHolder h, View v) {
		h.tongji_list_xmb = (TextView) v.findViewById(R.id.tongji_list_xmb);
		h.tongji_list_content = (TextView) v.findViewById(R.id.tongji_list_content);
		h.tongji_list_checkbox = (CheckBox) v.findViewById(R.id.tongji_list_checkbox);
		h.tongji_list_fwks = (TextView) v.findViewById(R.id.tongji_list_fwks);

	}

	public static class ViewHolder {
		TextView tongji_list_xmb;
		TextView tongji_list_content;
		TextView tongji_list_fwks;
		public CheckBox tongji_list_checkbox;
	}

	/**
	 * 获取被选中的数据
	 * 
	 * @return
	 * @author 王婷婷
	 * @date 2017年6月1日 下午2:56:02
	 */
	public String getCheckedDatas() {
		String ck_ids = "";
		int size = isSelected.size();
		Log.w("T---", "tt" + isSelected.size());
		Log.w("T---3", "tt" + list.size());
		if (size > 0) {
			for (int i = 0; i < size; i++) {
				if (isSelected.get(i)) {
					ck_ids = ck_ids + list.get(i).getId() + ",";
				}
			}
			if (!TextUtils.isEmpty(ck_ids)) {
				ck_ids = ck_ids.substring(0, ck_ids.length() - 1);
			}
		}

		return ck_ids;
	}

	/**
	 * 设置选中的数据
	 * 
	 * @return
	 * @author 王婷婷
	 * @date 2017年6月1日 下午2:56:13
	 */
	public void setCheckedDatas(String ckidS) {
		if (TextUtils.isEmpty(ckidS)) {
			return;
		}
		Log.w(QYINFO_JC, ckidS);
		String ck_ids[] = ckidS.split(",");
		int length = ck_ids.length;
		int size = isSelected.size();
		if (length > 0) {
			for (int i = 0; i < length; i++) {
				for (int j = 0; j < size; j++) {
					if (ck_ids[i].equals(list.get(j).getId())) {
						// listChecks.get(j).setChecked(true);
						Log.w(QYINFO_JC, ck_ids[i]+" iii "+j);
						getIsSelected().put(j, true);
					}
				}
			}

		}

	}
}
